package org.linlinjava.litemall.wx.web;

import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.linlinjava.litemall.db.util.BadWordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 专题服务
 */
@RestController
@RequestMapping("/wx/brand")
@Validated
public class WxBrandController {


    @Autowired
    private LitemallBrandService brandService;
    @Autowired
    private LitemallQualificationService qualificationService;
    @Autowired
    private LitemallConstantService constantService;
    @Autowired
    private LitemallBrandInfoService brandInfoService;
    @Autowired
    private LitemallRegionService regionService;
    @Autowired
    private LitemallServiceService serviceService;


    /**
     * @Description  获取全部的资质列表
     * @Author Riko
     * @Date 2019/9/24
     **/
    @GetMapping("qualification")
    public Object qualification() {
        return qualificationService.getAllQualification();
    }

    /**
     * @Description  获取全部的规模常量
     * @Author Riko
     * @Date 2019/9/24
     **/
    @GetMapping("constant")
    public Object constant() {
        return constantService.getAllConstant();
    }

    /**
     * 品牌列表
     * @param page 分页页数
     * @param limit 分页大小
     * @return 品牌列表
     */
    @GetMapping("list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallBrand> brandList = brandService.query(page, limit, sort, order);
        return ResponseUtil.okList(brandList);
    }

    /**
     * 品牌详情
     * @param id 品牌ID
     * @return 品牌详情
     */
    @GetMapping("detail")
    public Object detail(@NotNull Integer id) {
        LitemallBrand entity = brandService.findById(id);
        if (entity == null) {
            return ResponseUtil.badArgumentValue();
        }

        return ResponseUtil.ok(entity);
    }

    /**
     * @Description  获取公司首页地址
     * @Author Riko
     * @Date 2019/9/24
     **/
    @PostMapping("homepage")
    public Object homepage(@RequestBody String body) {
        Integer brandId = JacksonUtil.parseInteger(body, "brandId");
        if (brandId == null || brandId < 1) {
            return ResponseUtil.badArgument();
        }
        else {
            return ResponseUtil.ok(brandInfoService.getHomePageByBrandId(brandId));
        }
    }


    /**
     * @Description  获取公司详细信息
     * @Author Riko
     * @Date 2019/9/24
     **/
    @GetMapping("info")
    public Object info(HttpServletRequest request) {
        Integer brandId = brandInfoService.getBrandIdByUrl(request.getHeader("Referer"));
        LitemallBrandInfo info = brandInfoService.getInfoByBrandId(brandId);
        LitemallBrand brand = brandService.findById(brandId);
        Map<String, Object> result = new LinkedHashMap<>(2);
        result.put("basicInfo", brand);
        result.put("detailedInfo", info);
        return ResponseUtil.ok(result);
    }

    /**
     * @Description  获取团队成员
     * @Author Riko
     * @Date 2019/9/24
     **/
    @GetMapping("staff")
    public Object staff(HttpServletRequest request) {
        Integer brandId = brandInfoService.getBrandIdByUrl(request.getHeader("Referer"));
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("teamInfo", brandInfoService.getInfoByBrandId(brandId).getTeam());
        result.put("staffInfo", brandInfoService.getStaffByBrandId(brandId));
        return ResponseUtil.ok(result);
    }
    
    /**
     * @Description  获取公司资质轮播图
     * @Author Riko
     * @Date 2019/9/24
     **/
    @GetMapping("banner")
    public Object banner(HttpServletRequest request) {
        Integer brandId = brandInfoService.getBrandIdByUrl(request.getHeader("Referer"));
        return ResponseUtil.ok(brandInfoService.getQualificationByBrandId(brandId));
    }
    
    /**
     * @Description  获取公司检测项目（带自定义描述）
     * @Author Riko
     * @Date 2019/9/24
     **/
    @GetMapping("category")
    public Object category(HttpServletRequest request) {
        Integer brandId = brandInfoService.getBrandIdByUrl(request.getHeader("Referer"));
        return ResponseUtil.ok(brandInfoService.getCategoryByBrandId(brandId));
    }

    /**
     * @Description  获取公司首页图片
     * @Author Riko
     * @Date 2019/10/7
     **/
    @GetMapping("picture")
    public Object picture(HttpServletRequest request, @RequestParam(value = "type") String type) {
        Integer brandId = brandInfoService.getBrandIdByUrl(request.getHeader("Referer"));
        return ResponseUtil.ok(brandService.getPicturesByBrandId(brandId, type));
    }

    /**
     * @Description  新增一条评论
     * @Author Riko
     * @Date 2019/9/25
     **/
    @PostMapping("insertComment")
    public Object insertComment(HttpServletRequest request, @RequestBody String body) {
        Integer brandId = brandInfoService.getBrandIdByUrl(request.getHeader("Referer"));
        String comment = JacksonUtil.parseString(body, "comment");
        if (StringUtils.isEmpty(comment) || brandId == null || brandId < 1) {
            return ResponseUtil.badArgument();
        }
        String signature = JacksonUtil.parseString(body, "signature");
        String mail = JacksonUtil.parseString(body, "mail");
        String remark = JacksonUtil.parseString(body, "remark");
        comment = BadWordUtil.replaceBadWord(comment, BadWordUtil.maxMatchType, "*");
        brandInfoService.insertNewComment(brandId, signature, mail, comment, remark);
        return ResponseUtil.ok();
    }

    /**
     * @Description  分页查询所有评论
     * @Author Riko
     * @Date 2019/9/25
     **/
    @GetMapping("comment")
    public Object getComment(HttpServletRequest request, @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer pageSize) {
        Integer brandId = brandInfoService.getBrandIdByUrl(request.getHeader("Referer"));
        if (brandId == null || brandId < 1) {
            return ResponseUtil.badArgument();
        }
        else {
            List<LitemallBrandComment> list = brandInfoService.qurreyComment(brandId, page, pageSize);
            Integer totalNum = brandInfoService.getCommentNum(brandId);
            Map<String, Object> data = new HashMap<>(2);
            data.put("comment", list);
            data.put("totalNum", totalNum);
            return ResponseUtil.ok(data);
        }
    }

     /**
     * @Description  测试获取二级域名
     * @Author Riko
     * @Date 2019/9/25
     **/
    @GetMapping("testtesttest")
    public void test(HttpServletRequest request) {
        System.out.println(System.getProperty("l;"));
    }

    /**
     * 获取所有一级行政区划
     **/
    @GetMapping("region")
    public Object region() {
        List<LitemallRegion> provinceList = regionService.queryByPid(0);
        return ResponseUtil.okList(provinceList);
    }

    @PostMapping("/getCommonService")
    public Object getCommonService(Integer brandId){
        LitemallService service = serviceService.getDefaultService(brandId);
        return ResponseUtil.ok(service.getImId());
    }




}